{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new Plot(title: \"We Will Control the Title\", xLabel: \"Horizontal\", yLabel: \"Vertical\")\n",
    "def ys = [0, 1, 6, 5, 2, 8]\n",
    "def ys2 = [0, 2, 7, 6, 3, 8]\n",
    "plot << new Line(y: ys, width: 10, color: Color.red)\n",
    "plot << new Line(y: ys, width: 3, color: Color.yellow)\n",
    "plot << new Line(y: ys, width: 4, color: new Color(33, 87, 141), style: StrokeType.DASH, interpolation: 0)\n",
    "plot << new Line(y: ys2, width: 2, color: new Color(212, 57, 59), style: StrokeType.DOT)\n",
    "plot << new Line(y: [5, 0], x: [0, 5], style: StrokeType.LONGDASH)\n",
    "plot << new Line(y: [4, 0], x: [0, 5], style: StrokeType.DASHDOT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ch = new Crosshair(color: new Color(255, 128, 5), width: 2, style: StrokeType.DOT)\n",
    "pp = new Plot(crosshair: ch, omitCheckboxes: true,\n",
    "                  legendLayout: LegendLayout.HORIZONTAL, legendPosition: LegendPosition.TOP)\n",
    "def x = [1, 4, 6, 8, 10]\n",
    "def y = [3, 6, 4, 5, 9]\n",
    "pp << new Line(displayName: \"Line\", x: x, y: y, width: 3)\n",
    "pp << new Bars(displayName: \"Bar\", x: (1..10), y: [2, 2, 4, 4, 2, 2, 0, 2, 2, 4], width: 0.5)\n",
    "pp << new Points(x: x, y: y, size: 10, toolTip: {xs, ys -> \"x = \" + xs + \", y = \" + ys })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.fileloader.CSV\n",
    "rates = new CSV().read(\"../../../doc/resources/data/interest-rates.csv\")\n",
    "def size = rates.size()\n",
    "(0 ..< size).each{row = rates[it]; row.spread = row.y10 - row.m3}\n",
    "OutputCell.HIDDEN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new SimpleTimePlot(rates, [\"y1\", \"y10\"], // column names\n",
    "                   timeColumn : \"time\", // time is default value for a timeColumn\n",
    "                   yLabel: \"Price\", \n",
    "                   displayNames: [\"1 Year\", \"10 Year\"],\n",
    "                   colors : [[216, 154, 54], '#aabbcc'],\n",
    "                   displayLines: false, // no lines (true by default)\n",
    "                   displayPoints: true) // show points (false by default))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def cal = Calendar.getInstance();\n",
    "cal.add(Calendar.HOUR, -1)\n",
    "\n",
    "def today = new Date();\n",
    "def millis = today.time;\n",
    "def hour = 1000 * 60 * 60;\n",
    "\n",
    "def plot = new TimePlot(\n",
    "  timeZone: new SimpleTimeZone(10800000, \"America/New_York\")\n",
    ");\n",
    "\n",
    "//list of milliseconds\n",
    "plot << new Points(x:(0..10).collect{millis + hour * it}, y:(0..10), size: 10, displayName: \"milliseconds\");\n",
    "//list of java.util.Date objects\n",
    "plot << new Points(x:(0..10).collect{cal.add(Calendar.HOUR, 1); cal.getTime()}, y:(0..10), size: 4, displayName: \"date objects\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Generate some random data\n",
    "\n",
    "Random random = new Random();\n",
    "dataH1 = [];\n",
    "dataH2 = [];\n",
    "\n",
    "(1..10000).each {\n",
    "  dataH1 << random.nextGaussian();\n",
    "  dataH2 << 2*random.nextGaussian() + 1.0;\n",
    "}\n",
    "OutputCell.HIDDEN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new Histogram(initWidth:444, initHeight:400,\n",
    "              title:\"Wide Histogram with Manual Parameters\",\n",
    "              xLabel:\"Size\",\n",
    "              yLabel:\"Count\",\n",
    "              rangeMin: -8, \n",
    "              rangeMax: 8, \n",
    "              data: dataH1 + [7]*200,\n",
    "              binCount: 99, \n",
    "              color: new Color(0, 154, 166));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataHM  = [[533.08714795974, 484.92105712087596, 451.63070008303896, 894.4451947886148,   335.44965728686225, 640.9424094527392, 776.2709495045433, 621.8819257981404,   793.2905673902735, 328.97078791524234, 139.26962328268513, 800.9314566259062,   629.0795214099808, 418.90954534196544, 513.8036215424278, 742.9834968485734,   542.9393528649774, 671.4256827205828, 507.1129322933082, 258.8238039352692,   581.0354187924672, 190.1830169180297, 480.461111816312, 621.621218137835,   650.6023460248642, 635.7577683708486, 605.5201537254429, 364.55368485516846,   554.807212844458, 526.1823154945637],  [224.1432052432479, 343.26660237811336, 228.29828973027486, 550.3809606942758,   340.16890889700994, 214.05332637480836, 461.3159325548031, 471.2546571575069,   503.071081294441, 757.4281483575993, 493.82140462579406, 579.4302306011925,   459.76905409338497, 580.1282535427403, 378.8722877921564, 442.8806517248869,   573.9346962907078, 449.0587543606964, 383.50503527041144, 378.90761994599256,   755.1883447435789, 581.6815170672886, 426.56807864689773, 602.6727518023347,   555.6481983927658, 571.1201152862207, 372.24744704437876, 424.73180136220844,   739.9173564499195, 462.3257604373609],  [561.8684320610753, 604.2859791599086, 518.3421287392559, 524.6887104615442,   364.41920277904774, 433.37737233751386, 565.0508404421712, 533.6030951907703,   306.68809206630397, 738.7229466356732, 766.9678519097575, 699.8457506281374,   437.0340850742263, 802.4400914789037, 417.38754410115075, 907.5825538527938,   521.4281410545287, 318.6109350534576, 435.8275858900637, 463.82924688853524,   533.4069709666686, 404.50516534982546, 332.6966202103611, 560.0346672408426,   436.9691072984075, 631.3453929454839, 585.1581992195356, 522.3209865675237,   497.57041075817443, 525.8867246757814],  [363.4020792898871, 457.31257834906256, 333.21325206873564, 508.0466632081777,   457.1905718373847, 611.2168422907173, 515.2088862309242, 674.5569500790505,   748.0512665828364, 889.7281605626981, 363.6454276219251, 647.0396659692233,   574.150119779024, 721.1853645071792, 309.5388283799724, 450.51745569875845,   339.1271937333267, 630.6976744426033, 630.1571298446103, 615.0700456998867,   780.7843408745639, 205.13803869051543, 784.5916902014255, 498.10545868387925,   553.936345186856, 207.59216580556847, 488.12270849418735, 422.6667046886397,   292.1061953879919, 565.1595338825396],  [528.5186504364794, 642.5542319036714, 563.8776991112292, 537.0271437681837,   430.4056097950834, 384.50193545472877, 693.3404035076994, 573.0278734604005,   261.2443087970927, 563.412635691231, 258.13860041989085, 550.150017102056,   477.70582135030617, 509.4311099345934, 661.3308013433317, 523.1175760654914,   370.29659041946326, 557.8704186019502, 353.66591951113645, 510.5389425077261,   469.11212447314324, 626.2863927887214, 318.5642686423241, 141.13900677851177,   486.00711121264453, 542.0075639686526, 448.7161764573215, 376.65492084577164,   166.56246586635706, 718.6147921685923],  [435.403218786657, 470.74259129379413, 615.3542648093958, 483.61792559031693,   607.9455289424717, 454.9949861614464, 869.45041758392, 750.3595195751914,   754.7958625343501, 508.38715645396553, 368.2779213892305, 662.23752125613,   350.46366230046397, 619.8010888063362, 497.9560438683688, 420.64163974607766,   487.16698403905633, 273.3352931767504, 354.02637708217384, 457.9408818614016,   496.2986534025747, 364.84710143814976, 458.29907844925157, 634.073520178434,   558.7161089429649, 603.6634230782621, 514.1019407724017, 539.6741842214251,   585.0639516732675, 488.3003071211236],  [334.0264519516021, 459.5702037859653, 543.8547654459309, 471.6623772418301,   500.98627686914386, 740.3857774449933, 487.4853744264201, 664.5373560191691,   573.764159193263, 471.32565842016527, 448.8845519093864, 729.3173859836543,   453.34766656988694, 428.4975196541853, 575.1404740691066, 190.18782164376034,   243.90403003048107, 430.03959300145215, 429.08666492876233, 508.89662188951297,   669.6400651031191, 516.2894766192492, 441.39320293407405, 653.1948574772491,   529.6831617222962, 176.0833629734244, 568.7136007686755, 461.66494617366294,   443.39303344518356, 840.642834252332],  [347.676690455591, 475.0701395711058, 383.94468812449156, 456.7512619303556,   547.1719187673109, 224.69458657065758, 458.98685335259506, 599.8561007491281,   231.02565460233575, 610.5318803183029, 763.3423474509603, 548.8104762105211,   445.95788564834953, 844.6566709331175, 591.2236009653337, 586.0438760821825,   399.6820689195621, 395.17360423878256, 535.9853351258233, 332.27242110850426,   801.7584039310705, 190.6337233666032, 805.700536966829, 799.6824375238089,   346.29917202656327, 611.7423892505719, 705.8824305058062, 535.9691379719488,   488.1708623023391, 604.3772264289142],  [687.7108994865216, 483.44749361779685, 661.8182197739575, 591.5452701990528,   151.60961549943875, 524.1475889465452, 745.1142999852398, 665.6103992924466,   701.3015233859578, 648.9854638583182, 403.08097902196505, 384.97216329583586,   442.52161997463816, 590.5026536093199, 219.04366558018955, 899.2103705796073,   562.4908789323547, 666.088957218587, 496.97593850278065, 777.9572405840922,   531.7316118485633, 500.7782009017233, 646.4095967934252, 633.5713368259554,   608.1857007168994, 585.4020395597571, 490.06193749044934, 463.884131549627,   632.7981360348942, 634.8055942938928],  [482.5550451528366, 691.7011356960619, 496.2851035642388, 529.4040886765091,   444.3593296445004, 198.06208336708823, 365.6472909266031, 391.3885069938369,   859.494451604626, 275.19483951927816, 568.4478784631463, 203.74971298680123,   676.2053582803082, 527.9859302404323, 714.4565600799949, 288.9012675397431,   629.6056652113498, 326.2525932990075, 519.5740740263301, 696.8119752318905,   347.1796230415255, 388.6576994098651, 357.54758351840974, 873.5528483422207,   507.0189947052724, 508.1981784529926, 536.9527958233257, 871.2838601964829,   361.93416709279154, 496.5981745168124]]\n",
    "\n",
    "new HeatMap(title: \"Heatmap Second Example\",\n",
    "            xLabel:\"X Label\",\n",
    "            yLabel:\"Y Label\",\n",
    "            data: dataHM,\n",
    "            legendPosition: LegendPosition.TOP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// generate a random walk\n",
    "steps = 10000\n",
    "random = new Random()\n",
    "def walk(ssize) {\n",
    "  def cur = 0.0\n",
    "  def x = []\n",
    "  def y = []\n",
    "  for (i in 0..steps) {\n",
    "    y[i] = cur\n",
    "    x[i] = i\n",
    "    cur += random.nextGaussian() * ssize\n",
    "  }\n",
    "  return [x:x, y:y]\n",
    "}\n",
    "// now x is time for the x-axis, and y is the random variable\n",
    "beer = walk(10)\n",
    "whiskey = walk(100)\n",
    "OutputCell.HIDDEN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = new TimePlot(title: \"Drunken Sailor Walks\", showLegend: true, lodThreshold: 2000)\n",
    "p << new Line(x:beer.x, y:beer.y, displayName:\"Beer Walk\")\n",
    "p << new Line(x:whiskey.x, y:whiskey.y, displayName:\"Whiskey Walk\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node = new TreeMapNode(\"0\");\n",
    "def node00 = new TreeMapNode(\"00\", 2, new DefaultValue(2));\n",
    "\n",
    "def node01 = new TreeMapNode(\"01\");\n",
    "node01.add(new TreeMapNode(\"011\", 1, new DefaultValue(1)))\n",
    "node01.add(new TreeMapNode(\"012\", 2, new DefaultValue(1)))\n",
    "node01.add(new TreeMapNode(\"013\", 3, new DefaultValue(1)))\n",
    "\n",
    "def node02 = new TreeMapNode(\"02\");\n",
    "node02.add(new TreeMapNode(\"020\", 2, new DefaultValue(2)))\n",
    "node02.add(new TreeMapNode(\"021\", 1, new DefaultValue(1)))\n",
    "node02.add(new TreeMapNode(\"022\", 1, new DefaultValue(1)))\n",
    "node02.add(new TreeMapNode(\"023\", 1, new DefaultValue(1)))\n",
    "node02.add(new TreeMapNode(\"024\", 2, new DefaultValue(2)))\n",
    "\n",
    "def node03 = new TreeMapNode(\"03\");\n",
    "node03.add(new TreeMapNode(\"030\", 1, new DefaultValue(2)))\n",
    "node03.add(new TreeMapNode(\"031\", 2, new DefaultValue(2)))\n",
    "node03.add(new TreeMapNode(\"032\", 3, new DefaultValue(2)))\n",
    "node03.add(new TreeMapNode(\"033\", 4, new DefaultValue(2)))\n",
    "\n",
    "def node04 = new TreeMapNode(\"04\", 6, new DefaultValue(5))\n",
    "\n",
    "node.add(node01)\n",
    "node.add(node02)\n",
    "node.add(node03)\n",
    "node.add(node04)\n",
    "\n",
    "OutputCell.HIDDEN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def x = new TreeMap(\n",
    "  root: node,\n",
    "  showLegend: true,\n",
    "  title: \"Simple TreeChart\"\n",
    ");\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cplot = new CategoryPlot(title: \"Hello CategoryPlot!\",\n",
    "                             xLabel: \"Categories\",\n",
    "                             yLabel: \"Values\")\n",
    "cplot << new CategoryBars(value:[[1, 2, 3], [1, 3, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.table.*\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "\n",
    "display = new TableDisplay(new CSV().read(\"../../../doc/resources/data/interest-rates.csv\"))\n",
    "//show all time columns in days\n",
    "display.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "//min 4, max 6 decimal places for all doubles\n",
    "display.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(4,6))\n",
    "//setting for a column takes precidence over the type\n",
    "display.setStringFormatForColumn(\"m3\", TableDisplayStringFormat.getDecimalFormat(0, 0))\n",
    "//set the alignment\n",
    "display.setAlignmentProviderForType(ColumnType.Double, TableDisplayAlignmentProvider.RIGHT_ALIGNMENT)\n",
    "display.setAlignmentProviderForColumn('m3', TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "\n",
    "//using a closure\n",
    "display.setStringFormatForColumn(\"y3\") { value, row, col, tableDisplay ->\n",
    "   if(value < 8) {\n",
    "       \":(\"\n",
    "   } else {\n",
    "       \":)\"\n",
    "  } \n",
    "}\n",
    "\n",
    "display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.table.highlight.*\n",
    "\n",
    "def table = new TableDisplay([[1,2,3], \n",
    "                              [3,4,5], \n",
    "                              [6,2,8], \n",
    "                              [6,2,8], \n",
    "                              [6,2,8], \n",
    "                              [6,4,8], \n",
    "                              [6,2,8], \n",
    "                              [6,2,8], \n",
    "                              [6,5,8]], \n",
    "                             ['a', 'b', 'c'], \n",
    "                             ['double', 'double', 'double'])\n",
    "table.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter(\"b\", TableDisplayCellHighlighter.FULL_ROW))\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
